package com.sciencep.pro.admin.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sciencep.pro.admin.system.common.util.UserUtil;
import com.sciencep.pro.common.config.Constant;
import com.sciencep.pro.common.domain.ResponseResult;
import com.sciencep.pro.common.exception.CustomException;
import com.sciencep.pro.core.domain.dto.project.ProDataTO;
import com.sciencep.pro.core.domain.dto.user.UserDetail;
import com.sciencep.pro.core.domain.entity.DataInfo;
import com.sciencep.pro.core.service.IDataInfoService;
import com.sciencep.pro.core.service.IProDataService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/proDate")
@RestController
@Slf4j
public class ProDataController {

    @Autowired
    IProDataService proDataService;

    @Autowired
    IDataInfoService dataInfoService;

    @Autowired
    private UserUtil userUtil;

    @PostMapping(value = "/addProDate")
    @ApiOperation(value = "新增项目资料")
    public ResponseResult<Object> addProDate(@RequestBody ProDataTO proDataTO) throws CustomException {
        UserDetail userDetail = userUtil.getCurrentUserDetail();
        if(proDataService.addProDate(proDataTO,userDetail)){
            return new ResponseResult<>().success();
        }else {
            return new ResponseResult<>().failure();
        }
    }

    @DeleteMapping(value = "/delProDate")
    @ApiOperation(value = "删除项目资料")
    public ResponseResult<Object> delProDate(String id){
        if(proDataService.removeById(id)){
            return new ResponseResult<>().success();
        }else {
            return new ResponseResult<>().failure();
        }
    }

    @PostMapping(value = "updateProDate")
    @ApiOperation(value = "更新项目资料")
    public ResponseResult<Object> updateProDate(@RequestBody ProDataTO proDataTO) throws CustomException {
        UserDetail userDetail = userUtil.getCurrentUserDetail();
        if(proDataService.updateProDate(proDataTO,userDetail)){
            return new ResponseResult<>().success();
        }else {
            return new ResponseResult<>().failure();
        }
    }

    @GetMapping(value = "/findProNodeById")
    @ApiOperation(value = "根据项目id查询节点")
    public ResponseResult<Object> findProNodeById(@RequestParam String proId){
        return new ResponseResult<>().success(proDataService.findProNodeById(proId));
    }

    @GetMapping(value = "/findData")
    @ApiOperation(value = "查询项目资料")
    public ResponseResult<List<DataInfo>> findData(){
        return new ResponseResult<List<DataInfo>>().success(dataInfoService.list(new LambdaQueryWrapper<DataInfo>().eq(DataInfo::getIsDel, Constant.IsDelete.NO)));
    }

}
